package com.hnd.springframework.beans.factory.support;

import com.hnd.springframework.beans.BeanException;
import com.hnd.springframework.beans.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class SimpleInstantiationStrategy implements InstantitationStraegy {
    @Override
    public Object instantiate(String beanName, BeanDefinition beanDefinition, Constructor ctor, Object... args) {
        Class clazz = beanDefinition.getBeanClass();
        try {
            if (ctor == null) {
                return clazz.newInstance();
            }
            return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
        } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
            throw new BeanException("bean 实例化失败", e);
        }

    }
}
